package gdma.hqdes.bean;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonIgnore;
import common.lang.TreeNode;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.ToString;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @Author wangyue
 * @Version 3.0
 * @Date 2024-07-13
 */

@Data
public class HqdIndex implements TreeNode<HqdIndex>, IndexInfo, Serializable {

    @ApiModelProperty("id")
    @TableId
    Long id;

    @ApiModelProperty("版本id")
    Integer verId;

    @ApiModelProperty("指标级别：0=综合指标，1=一级指标，2=二级指标，3=三级指标")
    Integer level;

    @ApiModelProperty("指标代码，见《广东市县级气象高质量发展评价办法（2023—2025年）》中的定义")
    String code;

    @ApiModelProperty("名称")
    String name;

    @ApiModelProperty("上级指标项Id")
    Long parentId;

    @ApiModelProperty("权重0-100")
    Float weight;

    @ApiModelProperty("创建时间")
    Date createdTime;

    @ApiModelProperty("更新时间")
    Date updatedTime;

    @TableField(exist = false)
    List<HqdIndexExt> indexExts;

    public HqdIndexExt getIndexExt(String areaTag) {
        Optional<HqdIndexExt> found = indexExts.stream().filter(ext -> StringUtils.hasText(ext.getAreaTag()) && ext.getAreaTag().equals(areaTag)).findAny();
        if(found.isPresent()) return found.get();
        found = indexExts.stream().filter(ext -> !StringUtils.hasText(ext.getAreaTag())).findAny();
        return found.isPresent() ? found.get() : null;
    }

    @TableField(exist = false)
    List<HqdIndexArg> indexArgs;

    @TableField(exist = false)
    @JsonIgnore
    @ToString.Exclude
    HqdIndex parent;

    @TableField(exist = false)
    List<HqdIndex> children;

    public void addChild(HqdIndex child) {
        if(children == null) children = new ArrayList<>();
        children.add(child);
    }

    @TableField(exist = false)
    UnitScore unitScore;

}
